package cn.wolfcode.luowowo.website.web.controller;


import cn.wolfcode.luowowo.article.domain.StrategyCommend;
import cn.wolfcode.luowowo.article.domain.TravelCommend;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.ITravelCommendService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.common.util.RedisKeys;
import cn.wolfcode.luowowo.member.domain.Userinfo;
import cn.wolfcode.luowowo.search.domain.DestinationTemplate;
import cn.wolfcode.luowowo.search.domain.StrategyTemplate;
import cn.wolfcode.luowowo.search.domain.TravelTemplate;
import cn.wolfcode.luowowo.search.domain.UserInfoTemplate;
import cn.wolfcode.luowowo.search.qo.IndexSearchQo;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.vo.IndexStaticVo;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class IndexController {

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Reference
    private ITravelCommendService travelCommendService;
    @Reference
    private IStrategyCommendService strategyCommendService;
    @Reference
    private ITravelService travelService;
    @Reference
    private IDestinationTemplateService destinationTemplateService;
    @Reference
    private ITravelTemplateService travelTemplateService;
    @Reference
    private IUserInfoSearchService userInfoTemplateService;
    @Reference
    private IStrategyTemplateService strategyTemplateService;
    @Reference
    private ISearchService searchService;
    @RequestMapping("")
    public String ifSuccessLogin(HttpServletRequest request, Model model) {
            //判断 cookie中是否有userinfo
        String key=null;
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            String cookieName = cookie.getName();
            if("token".equals(cookieName)) {
                //再次访问  所以要重新设置时间
                cookie.setMaxAge(RedisKeys.USERINFO_TOKEN.getTimeout());
                String result = cookie.getValue();
                key=result;
                break;
            }
        }

        if(key!=null) {
            //有的话 放到session里面
            String userinfo_str = redisTemplate.opsForValue().get(key);
            Userinfo userinfo = JSON.parseObject(userinfo_str, Userinfo.class);
            request.getSession().setAttribute("userInfo",userinfo);
        }
        //tcs
        List<TravelCommend> tcs = travelCommendService.getTop5();
        model.addAttribute("tcs",tcs);
        //sc
        StrategyCommend sc=strategyCommendService.getTop5().get(0);
        model.addAttribute("sc",sc);
        return "index/index";
    }

    @RequestMapping("/index/travelPage")
    public String travelPage(@ModelAttribute("qo") TravelQuery qo,Model model) {
        PageInfo pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "index/travelPageTpl";
    }

    @RequestMapping("/q")
    public String q(@ModelAttribute("qo") IndexSearchQo qo,Model model) {
            switch (qo.getType()) {
                case "0":
                    return searchDestinaiton(qo,model);
                case "1":
                    return searchStrategy(qo,model);
                case "2":
                    return searchTravel(qo,model);
                case "3":
                    return searchUserInfo(qo,model);
            }
            return searchAll(qo,model);
    }

    public String searchAll(IndexSearchQo qo,Model model) {
        qo.setType(Integer.toString(IndexSearchQo.SEARCH_TYPE_DEST));
       Page pageD=searchService.searchWithHighlight(DestinationTemplate.INDEX_NAME,DestinationTemplate.TYPE_NAME, DestinationTemplate.class, qo, "name","info");
        long d = pageD.getTotalElements();
        IndexStaticVo vo=new IndexStaticVo();
            vo.setDests(pageD.getContent());//"title", "subTitle", "summary"
        qo.setType(Integer.toString(IndexSearchQo.SEARCH_TYPE_STRATEGY));
            Page pageS=searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME,StrategyTemplate.TYPE_NAME, DestinationTemplate.class, qo, "title", "subTitle", "summary");
            vo.setStrategys(pageS.getContent());
            Long a = pageS.getTotalElements();//"title","summary"
        qo.setType(Integer.toString(IndexSearchQo.SEARCH_TYPE_TRAVEL));
            Page pageT=searchService.searchWithHighlight(TravelTemplate.INDEX_NAME,TravelTemplate.TYPE_NAME, DestinationTemplate.class, qo, "title","summary");
            vo.setTravels(pageT.getContent());
            Long b = pageT.getTotalElements();//"nickname","destName"
        qo.setType(Integer.toString(IndexSearchQo.SEARCH_TYPE_USER));
            Page pageU=searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME,UserInfoTemplate.TYPE_NAME, DestinationTemplate.class, qo, "nickname","destName");
            vo.setUsers(pageU.getContent());
            Long c = pageU.getTotalElements();
            vo.setTotal(a+b+c+d);
            model.addAttribute("data",vo);
        return "index/searchAll";
    }

    public String searchDestinaiton(IndexSearchQo qo,Model model) {
        DestinationTemplate dest=destinationTemplateService.queryWithHighlight(qo);
        if(dest!=null) {
            IndexStaticVo vo=new IndexStaticVo();
            qo.setType(Integer.toString(IndexSearchQo.SEARCH_TYPE_STRATEGY));
            Page pageS=strategyTemplateService.queryWithHighlight(qo);
            vo.setStrategys(pageS.getContent());
            Long a = pageS.getTotalElements();
            qo.setType(Integer.toString(IndexSearchQo.SEARCH_TYPE_TRAVEL));
            Page pageT=travelTemplateService.queryWithHighlight(qo);
            vo.setTravels(pageT.getContent());
            Long b = pageT.getTotalElements();
            qo.setType(Integer.toString(IndexSearchQo.SEARCH_TYPE_USER));
            Page pageU=userInfoTemplateService.queryWithHighlight(qo);
            vo.setUsers(pageU.getContent());
            Long c = pageU.getTotalElements();
            vo.setTotal(a+b+c);
            model.addAttribute("data",vo);
            model.addAttribute("dest",dest);
        }
        return "index/searchDest";
    }

    public String searchStrategy(IndexSearchQo qo,Model model) {
        Page page=strategyTemplateService.queryWithHighlight(qo);
        model.addAttribute("page",page);
        return "index/searchStrategy";
    }

    public String searchTravel(IndexSearchQo qo,Model model) {
        Page page=travelTemplateService.queryWithHighlight(qo);
        model.addAttribute("page",page);
        return "index/searchTravel";
    }

    public String searchUserInfo(IndexSearchQo qo,Model model) {
        Page page=userInfoTemplateService.queryWithHighlight(qo);
        model.addAttribute("page",page);
        return "index/searchUser";
    }
}
